package com.joymeter.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.joymeter.data.entity.GatewayReqParams;
import com.joymeter.data.entity.GatewayResParams;

import java.net.URLDecoder;

/**
 * @ClassName GatewayJsonFormat
 * 网关发送json格式化
 * @Description TODO
 * @Author liang
 * @Date 2018/8/22 9:31
 * @Version 1.0
 **/
public class GatewayJsonFormat {
    /**
     * 读表查询格式
     * @param data
     * @return
     */
    public static String readFormat(GatewayReqParams data){
        String isAutoClear = data.getIsAutoClear();
        String protocol = data.getProtocol();
        String dtuId = data.getDtuId();
        String meter = data.getMeter();
        String accountName = data.getAccountName();
        String action = data.getAction();
        String category = data.getCategory();
        String url = data.getUrl();
        if (EmptyUtils.isEmpty(isAutoClear) || EmptyUtils.isEmpty(protocol) || EmptyUtils.isEmpty(dtuId) || EmptyUtils.isEmpty(meter) || EmptyUtils.isEmpty(accountName) || EmptyUtils.isEmpty(category)|| EmptyUtils.isEmpty(url)) return null;
        //添加嵌套元素
        JSONObject DTUObj = new JSONObject();
        JSONArray DTUValueArr = new JSONArray();
        JSONObject optionsObj = new JSONObject();
        //配置项参数，isAutoClear为10表示开关阀，1表示抄表
        optionsObj.put("isAutoClear",isAutoClear);
        optionsObj.put("protocol", protocol);
        optionsObj.put("dtuId",dtuId);
        optionsObj.put("sendTime",System.currentTimeMillis());
        optionsObj.put("balanceWarnning","null");
        optionsObj.put("valveClose","null");
        optionsObj.put("concentrator_model","2");
        //回调时观察
        optionsObj.put("meter",meter);
        optionsObj.put("accountName",accountName);
        optionsObj.put("action",action);
        //表参数
        JSONArray metersArray = new JSONArray();
        JSONObject meters1 = new JSONObject();
        meters1.put("meter",meter);
        meters1.put("category",category);
        meters1.put("protocol",protocol);
        metersArray.add(meters1);
        //DTU数组对应的值，复杂形式json对象
        JSONObject info = new JSONObject();
        info.put("id",dtuId);
        info.put("url",url);
        info.put("options",optionsObj);
        info.put("meters",metersArray);
        DTUValueArr.add(info);
        DTUObj.put("DTU",DTUValueArr);
        return DTUObj.toString();
    }

    /**
     * 阀门请求格式化
     * @param data
     * @return
     */
    public static String valveFormat(GatewayReqParams data){
        String isAutoClear = data.getIsAutoClear();
        String protocol = data.getProtocol();
        String dtuId = data.getDtuId();
        String meter = data.getMeter();
        String accountName = data.getAccountName();
        String action = data.getAction();
        String category = data.getCategory();
        String url = data.getUrl();
        if (EmptyUtils.isEmpty(action) || EmptyUtils.isEmpty(isAutoClear) || EmptyUtils.isEmpty(protocol) || EmptyUtils.isEmpty(dtuId) || EmptyUtils.isEmpty(meter) || EmptyUtils.isEmpty(accountName) || EmptyUtils.isEmpty(category)|| EmptyUtils.isEmpty(url)) return null;
        //添加嵌套元素
        JSONObject DTUObj = new JSONObject();
        JSONArray DTUValueArr = new JSONArray();
        JSONObject optionsObj = new JSONObject();
        //配置项参数，isAutoClear为10表示开关阀，1表示抄表
        optionsObj.put("isAutoClear",isAutoClear);
        optionsObj.put("protocol", protocol);
        optionsObj.put("dtuId",dtuId);
        optionsObj.put("sendTime",System.currentTimeMillis());
        optionsObj.put("balanceWarnning","null");
        optionsObj.put("valveClose","null");
        optionsObj.put("concentrator_model","2");
        //回调时观察
        optionsObj.put("meter",meter);
        optionsObj.put("accountName",accountName);
        optionsObj.put("action",action);
        //表参数
        JSONArray metersArray = new JSONArray();
        JSONObject meters1 = new JSONObject();
        meters1.put("meter",meter);
        meters1.put("category",category);
        meters1.put("protocol",protocol);
        metersArray.add(meters1);
        //DTU数组对应的值，复杂形式json对象
        JSONObject info = new JSONObject();
        info.put("id",dtuId);
        info.put("url",url);
        info.put("options",optionsObj);
        info.put("meters",metersArray);
        DTUValueArr.add(info);
        DTUObj.put("DTU",DTUValueArr);
        return DTUObj.toString();
    }

    //json解码
    public static String toURLDecoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            //LogD("toURLDecoded error:"+paramString);
            return "";
        }

        try
        {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLDecoder.decode(str, "UTF-8");
            return str;
        }
        catch (Exception localException)
        {
            //LogE("toURLDecoded error:"+paramString, localException);
        }

        return "";
    }
    public static GatewayResParams readCallBack(String data){
        String decoded = toURLDecoded(data);
        JSONObject callBackObject = JSONObject.parseObject(decoded);
        String result =  callBackObject.getString("result");
        String data1 = callBackObject.getString("data1");
        String meter = callBackObject.getString("meter");
        String date = callBackObject.getString("date");
        GatewayResParams gatewayResParams = new GatewayResParams();
        gatewayResParams.setResult(result);
        gatewayResParams.setData(data1);
        gatewayResParams.setMeter(meter);
        gatewayResParams.setDate(date);
        return  gatewayResParams;
    }

    public static GatewayResParams valveCallBack(String data){
        String decoded = toURLDecoded(data);
        JSONObject callBackObject = JSONObject.parseObject(decoded);
        String result =  callBackObject.getString("result");
        String data1 = callBackObject.getString("data1");
        String meter = callBackObject.getString("meter");
        String date = callBackObject.getString("date");
        JSONObject options = callBackObject.getJSONObject("options");
        String action = options.getString("action");
        GatewayResParams gatewayResParams = new GatewayResParams();
        gatewayResParams.setResult(result);
        gatewayResParams.setData(data1);
        gatewayResParams.setMeter(meter);
        gatewayResParams.setDate(date);
        gatewayResParams.setAction(action);
        return  gatewayResParams;
    }


}
